package com.tanglongan.concurrent.thread;

public class JoinDemo {

    public static final int SLEEP_GAP = 5000;   //睡眠时长
    public static final int MAX_TURN = 50;      //睡眠次数

    static class SleepThread extends Thread {
        static int threadSeqNumber = 1;

        public SleepThread() {
            super("sleepThread " + (threadSeqNumber++));
        }

        @Override
        public void run() {
            System.out.println(getName() + "=====>进入睡眠");
            try {
                Thread.sleep(SLEEP_GAP);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println(getName() + " 发生被异常中断");
                return;
            }
            System.out.println(getName() + "- 运行结束. ");
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new SleepThread();
        System.out.println("启动 thread1.");
        thread1.start();

        try {
            thread1.join(); //main线程等待thread1，不限时
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread thread2 = new SleepThread();
        System.out.println("启动 thread2.");
        thread2.start();
        try {
            thread2.join(); //main线程等待thread2，不限时
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程运行结束.");
    }
}
